package com.jiawei;

import com.jiawei.mapper.TableInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.mybatis.spring.annotation.MapperScan;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
import java.util.Map;

/**
 * @author : willian fu
 * @version : 1.0
 */
@SpringBootApplication
@MapperScan("com.jiawei.mapper")
public class MainStater implements CommandLineRunner {
    @Autowired
    TableInfoMapper tableInfoMapper;

    public static void main(String[] args) {
        SpringApplication.run(MainStater.class,args);
    }

    @Override
    public void run(String... args) throws Exception {
        String dbName = "nengchenDB";
        List<Map<String,String>> tableList = tableInfoMapper.selectTableInfo(dbName);

        if (tableList.size()>1) {
            File file = new File("C:/Users/Administrator/Desktop/" + dbName + "数据库设计文档.md");
            if (!file.exists()) {
                file.createNewFile();
            }
            Writer writer = new FileWriter(file);
            writer.append("# 能臣IT数据库文档\n");

            writer.append("\n\n### 表总览  \n");
            tableList.forEach(tableInfo ->{
                try {
                    writer.append("**");
                    writer.append(String.valueOf(tableInfo.get("table_name"))+"**\n");
                    writer.append("> "+String.valueOf(tableInfo.get("table_comment"))+"\n\n");

                } catch (IOException e) {
                    e.printStackTrace();
                }
            });

            tableList.forEach(tableInfo -> {
                //获取表名
                String tableName = String.valueOf(tableInfo.get("table_name"));
                String tableComment = String.valueOf(tableInfo.get("table_comment"));
                try {
                    writer.append("\n\n## "+tableName+"【"+tableComment+"】\n");
                    writer.append("> "+tableComment+"\n");
                    //根据表名获取表字段结构信息
                    List<Map<String, String>> contents = tableInfoMapper.selectTableContent(dbName+"."+tableName);
                    writer.append(createTable(contents));
                } catch (Exception e) {
                    e.printStackTrace();
                }

                System.out.println(tableInfo.get("table_name") + "=>" + tableInfo.get("table_comment"));

            });
            writer.close();
        }
        System.out.println("退出");
    }
    public String createTable(List<Map<String, String>> contents){
        StringBuilder table = new StringBuilder();
        table.append("| 字段名 | 类型 | Key | 是否可以为空 | 说明/注释 |\n");
        table.append(outputTable(5,null));
        contents.forEach(content ->{
            table.append(outputTable(5,content.get("Field"),content.get("Type"),content.get("Key"), "YES".equals(content.get("Null"))?"是":"否",content.get("Comment").replaceAll("\r\n","，")));
        });
        table.append("\n\n");
        return table.toString();
    }
    public String outputTable(int column, String...content){
        StringBuilder columns = new StringBuilder();
        if (content == null){
            for (int i = 0; i < column; i++) {
                columns.append("| ------ ");
            }
        }else {
            for (String s : content) {
                columns.append("| ");
                columns.append(s);
                columns.append(" ");
            }
        }
        columns.append("|\n");
        return columns.toString();
    }
}
